package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.domain.UserLogin;
import cn.wolfcode.shop.service.IUserLoginService;
import cn.wolfcode.vo.JSONResultVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.catalina.connector.Response;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/users")
public class UserLoginController {

    @Reference
    private IUserLoginService userLoginService;


    /**
     * 注册用户接口
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping
    public JSONResultVo register(String username, String password) {

        UserLogin user = userLoginService.register(username, password);

        return JSONResultVo.instance();
    }

    /**
     * 用户登录
     *
     * @return
     */
    @PostMapping("/tokens")
    public JSONResultVo login(String username, String password) {
        userLoginService.login(username, password);
        return JSONResultVo.instance();
    }

    /**
     * @return
     * @RequestHeader(name = "userToken")
     * 注销账户
     */
    @DeleteMapping("/tokens")
    public JSONResultVo logout(@RequestHeader(name = "userToken") String token, HttpServletResponse response) {

        userLoginService.logout(token);

        //删除完后设置204的响应码
        response.setStatus(Response.SC_NO_CONTENT);
        return JSONResultVo.instance();
    }


}
